﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 切换标题的外虚框（圆角矩形）。
    /// ★①，修改类名。
    /// </summary>
    public static class SwitchTitleOutBorderCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SwitchTitleOutBorderCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SwitchTitleOutBorderCommand",
                "SwitchTitleOutBorderCommand",
                typeof(SwitchTitleOutBorderCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.B,ModifierKeys.Control|ModifierKeys.Shift,"Ctrl+Shift+B")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。
            Execute();
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.MainWindow.SelectedTitlesList == null ||
                Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                e.CanExecute = false; return;
            }

            if (Globals.MainWindow.SelectedTitlesList.Count > 1)
            {
                e.CanExecute = false; return;
            }
            else
            {
                if (Globals.MainWindow.SelectedTitlesList[0] == Globals.MainWindow.RootTitle)
                {
                    e.CanExecute = false; return;
                }

                e.CanExecute = true; return;
            }
        }

        public static void Execute()
        {
            //if (Globals.MainWindow.SelectedTitlesList.Count == 1)
            //{
            Title curTitle = Globals.MainWindow.MainSelectedTitle;
            if (curTitle == null)
            {
                MessageBox.Show("　　清先选定一个标题。", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curTitle == Globals.MainWindow.RootTitle)
            {
                MessageBox.Show("　　根标题（中心标题）不允许显示外边框。", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curTitle.IsFixed)
            {
                MessageBox.Show("　　自适应（FixedTitle）不允许显示外边框。", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem();

            bool newValue = !curTitle.IsOutBorderVisibled;

            Action act = new Action(curTitle.ID, -1, curTitle.GetType().Name, "IsOutBorderVisibled",
                curTitle.IsOutBorderVisibled.ToString(), newValue.ToString());

            curTitle.IsOutBorderVisibled = newValue;

            mi.Add(act);

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            //}
        }
    }
}
